'''
Created on 16-01-2011

@author: lisu
'''
import unittest
from structures.grofile import GroAtom, Molecule, GroFrame


class TestGroAtom(unittest.TestCase):
    def setUp(self):
        self.atom = GroAtom('    1POPC    C1    3   1.561   3.310   1.537')
        self.atom2 = GroAtom('C', 2)
        
    def testConstructors(self):
        self.assertAlmostEqual(self.atom.x, 1.561)
        self.assertAlmostEqual(self.atom.y, 3.310)
        self.assertAlmostEqual(self.atom.z, 1.537)
        self.assertAlmostEqual(self.atom.number, 3)
        
        self.assertEqual(self.atom2.symbol, 'C')
        self.assertAlmostEqual(self.atom2.x, 0.)
    
    def test_v0(self):
        self.assertEquals(self.atom2.v[0], 0)
        self.assertEquals(self.atom2.v[2], 0)
        self.atom.v = [1,2,3]
        self.assertEquals(self.atom.v[2], 3)
        
class TestMolecule(unittest.TestCase):
    def setUp(self):
        self.mol = Molecule('DOPC')
    def testConstructors(self):
        self.assertEquals(self.mol.resname, "DOPC")
        
        self.mol.resname = "POPC"
        self.assertEquals(self.mol.resname, "POPC")

class TestGroFrame(unittest.TestCase):
    def setUp(self):
        self.frameStr = """Generated by trjconv : popc_pops t=   0.00000
 10
    1POPC    C1    1   1.561   3.310   1.537
    1POPC    C2    2   1.425   3.175   1.678
    1POPC    C3    3   1.557   3.347   1.771
    1POPC    N4    4   1.555   3.243   1.668
    1POPC    C5    5   1.668   3.152   1.689
    2POPC    C1    6   1.561   3.310   1.537
    2POPC    C2    7   1.425   3.175   1.678
    2POPC    C3    8   1.557   3.347   1.771
    2POPC    N4    9   1.555   3.243   1.668
    2POPC    C5   10   1.668   3.152   1.689
   6.53265   6.53265   7.05680"""
        self.frame = GroFrame(self.frameStr)
    
    def test_construct(self):
        self.assertEquals(len(self.frame.atoms), 10)
        self.assertEquals(len(self.frame.molecules), 2)
        self.assertAlmostEqual(self.frame.boxSize[1], 6.53265)
        for atomIndex in range(len(self.frame.atoms)):
            self.assertEquals(self.frame.atoms[atomIndex].number, atomIndex + 1)
        
    def test_repr(self):
        reprStr = self.frame.__repr__()
        
        self.assertEqual(13, len(reprStr.split('\n')))
        self.frame.molecules[0].atoms.append(GroAtom('P'))
        reprStr = self.frame.__repr__()
        self.assertEqual(14, len(reprStr.split('\n')))
        self.assertEquals(self.frame.molecules[0].atoms[-1].number, 6)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()